// pages/index/index.js
var QRCode = require('../../utils/weapp_qrcode.js')
var AsynGetOpenid = require('../../utils/AsynGetOpenid.js')
var qrcode;

const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
      Name: '',
      UserId: '',
      Cop_name: '',
      intervals: '',
      Cop_name: '我的机构',
      QRflage: true
    },

    /**
     * 生命周期函数--监听页面加载
     */

    onLoad: function(options) {
      qrcode = new QRCode('canvas', {
        // usingIn: this,
        width: 250,
        height: 250,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

      var _this = this

      if (wx.getStorageSync('cop_name')=='') {
        _this.setData({
          Cop_name: '我的学校'
        })
      } else {
        _this.setData({
          Cop_name: wx.getStorageSync('cop_name')
        })
      }
      if (wx.getStorageSync('openId') == '' || wx.getStorageSync('userId') == '') //判断是否有用户信息 如果没有则进行绑定
      {
        AsynGetOpenid.get_openidinfor(this.codecallback)
      }
      else{
        var openid = wx.getStorageSync('openId')
        var cop_id = wx.getStorageSync('cop_id')
        AsynGetOpenid.getcode(openid,cop_id,this.codecallback)

      }
      
      this.data.intervals = setInterval(function() {
        if (wx.getStorageSync('openId') != '') {
          _this.get_code()
        }
      }, 1000 * 55)

    },
    codecallback: function(res) {
      console.log('回调')
      console.log(res)
      qrcode.makeCode(res)
      var _this = this
      _this.setData({
        QRflage: false,
        Cop_name: wx.getStorageSync('cop_name')
      })
    },

    get_code: function() {
      if (wx.getStorageSync('openId') == '') //判断是否有用户信息 如果没有则进行绑定
      {
        AsynGetOpenid.get_openidinfor(this.codecallback)
      } else {
        var openid = wx.getStorageSync('openId')
        var cop_id = wx.getStorageSync('cop_id')
        AsynGetOpenid.getcode(openid, cop_id, this.codecallback)
      }
    },


    tobindpage: function() {
      var UserId = wx.getStorageSync('userId')
      console.log(UserId)
      if (UserId === '') {
        // 关闭定时器
        clearInterval(this.data.intervals)
        console.log(3)
        wx.navigateTo({
          url: '../../pages/selectpage/selectpage'
        })
      }
    },

    schoollist: function() {
      clearInterval(this.data.intervals)
      wx.switchTab({
        url: '../../pages/selectpage/selectpage'
      })
    },

    userinfo: function() {
      clearInterval(this.data.intervals)
      wx.navigateTo({
        url: '../../pages/my/my'
      })

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {
      clearInterval(this.data.intervals)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {
      clearInterval(this.data.intervals)

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
  }

)